﻿#region Copyright 2009 by Mike Podonyi, Licensed under the GNU Lesser General Public License
/*  This file is part of Console.Net.

 *  Console.Net is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.

 *  Console.Net is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.

 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Console.Net.  If not, see <http://www.gnu.org/licenses/>.
 */
#endregion

using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using Empi.Consolenet;

namespace Empi.Consolenet
{
   

    public static class App
    {
       


        //public static void Run(string filePath)
        //{
        //    var parser=new Parser(XDocument.Load(filePath));

        //    if (parser.Validate().HasMessage)
        //        Environment.Exit(1);

        //    Message message;
        //    CommandBase command= parser.Parse(out message);

        //}

       
        public static bool Validate(XDocument document, EventHandler<ConsoleValidationEventArgs> validationEventHandler)
        {
            if(document==null)
                throw new NullReferenceException();

            Validation validation = new Validation(new DataBucket(document), false, validationEventHandler);
            return validation.Validate();
        }

        public static void Validate(XDocument document)
        {
            Validate(document,null);
        }

        public static ICommand Parse(XDocument document)
        {
            return Parse(document, Environment.GetCommandLineArgs());
        }

#if TEST
        public
#else
        private
#endif
        static ICommand Parse(XDocument document, string[] args)
        {
            return Parse(document, args, null);
        }

        private static ICommand Parse(XDocument document, string[] args, EventHandler<ConsoleParsingEventArgs> parsingEventHandler)
        {
            if (document == null)
                throw new NullReferenceException();
            if (args == null)
                throw new NullReferenceException();
            if (args.Count() == 0)
                throw new FormatException("The argument array has to have at least one item.");

            DataBucket bucket = new DataBucket(document);

            Validation validation = new Validation(bucket, true, null);
            validation.Validate();

            CommandDom commandDom = CommandDomBuilder.GetCommandDom(bucket);

            var parser = new Parser(commandDom, args);

            parser.Parse();

            return commandDom;

//            if (document == null)
//                throw new NullReferenceException();
//            if(args==null)
//                throw new NullReferenceException();
//            if(args.Count()==0)
//                throw new FormatException("The argument array has to have at least one item.");

//            DataBucket bucket=new DataBucket(document);

//            Validation validation = new Validation(bucket, true, null);
//            try
//            {
//                validation.Validate();
//            }
//            catch (ConsoleValidationException exc)
//            {
//                //parsingEventHandler;
//            }
            

//            var parser = new Parser(bucket, args);

//            Message message;
//            CommandBase cmd=parser.Parse(out message);

//            if(message.HasMessage)
//            {
////                parsingEventHandler(null, new ConsoleValidationEventArgs(new ConsoleValidationException(message.MessageType, message.MessageValues), SeverityType.Error));
//                return null;
//            }

//            return cmd;
        }
    }
}